package com.starbaba.core.web.taglib.component;

import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.components.Component;
import org.apache.struts2.util.MakeIterator;

import com.starbaba.core.web.translate.TranslateUtils;
import com.starbaba.core.web.translate.TranslateUtilsCacheDecorator;
import com.starbaba.core.web.translate.TranslateUtilsFactory;
import com.starbaba.core.web.translate.TranslateVO;
import com.opensymphony.xwork2.util.ValueStack;

public class CommonSelectComponent2 extends Component {

	static Logger logger = Logger.getLogger(CommonSelectComponent2.class);
	private Object list;
	private String id;
	private String name;
	private String value;
	private String emptyOption;
	private String required;
	private String headerKey;
	private String headerValue;

	public CommonSelectComponent2(ValueStack stack) {
		super(stack);
	}

	@Override
	public boolean start(Writer writer) {
		boolean result = super.start(writer);
		try {
			String targetName = this.getOutputMessage();
			if (StringUtils.isNotBlank(targetName)) {
				writer.write(targetName);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return result;
	}

	public void evaluateExtraParams() {

	}

	public List<TranslateVO> getTranslateVOList() {
		try {
			TranslateUtils translateUtils = TranslateUtilsFactory.getInstance();
			if (null == translateUtils) {
				logger.error("TranslateUtils[definition=" + list + "]");
			}
			return new TranslateUtilsCacheDecorator(translateUtils).getCollection((String) list);
		} catch (Exception ex) {
			logger.error("TranslateUtils[definition=" + list + "]");
			logger.error(ex.getMessage());
		}
		return null;
	}

	public String getOutputMessage(){
		StringBuffer selectHtml = new StringBuffer();
		try{
				if (list == null) {
			        list = parameters.get("list");
			    }
				
		        if (list instanceof String) {
		        	String definition = (String) list;
		        	if (StringUtils.isNotBlank(definition) 
		        			&& (definition.startsWith(TranslateUtils.SYSCODE_PREFIX) || definition.startsWith(TranslateUtils.REFLECT_PREFIX))
		        			&& definition.indexOf(TranslateUtils.BRACES) < 0){
		        		
		        		TranslateVO emptyOptionItem = new TranslateVO(this.getHeaderValue(),this.getHeaderKey());
		        		List<TranslateVO> selectItems = new ArrayList<TranslateVO>();
		        		String selectText = TranslateUtilsFactory.getInstance().getName(definition,value,false);
		        		if(StringUtils.isBlank(selectText)){
		        			selectText = this.getHeaderKey();
		        			if(!"false".equals(emptyOption)){
		        				selectItems.add(emptyOptionItem);
		        			}
		        		}else{
		        			if(!"false".equals(emptyOption)){
		        				selectItems.add(emptyOptionItem);
		        			}
		        		}
		        		
		        		List<TranslateVO> translateList = this.getTranslateVOList();
		        		//如果不允许为空，则默认显示第一个选项
		        		if("false".equals(emptyOption) && translateList != null && StringUtils.isBlank(selectText)){//emptyOption为false,默认选择第一项
		        			TranslateVO defaultItem = translateList.get(0);
		        			if(defaultItem!=null){
		        				selectText = defaultItem.getName();
				        			value = defaultItem.getCode();
		        			}
	        			}
		        		selectItems.addAll(translateList);
		        		
		        		if("true".equals(required)){
		        			selectHtml.append("<div class='ui-ddlist required' id='"+id+"'>");
		        		}else{
		        			selectHtml.append("<div class='ui-ddlist' id='"+id+"'>");
		        		}
		        		selectHtml.append("     <div class='ui-ddlist-text'>"+selectText+"</div>");
		        		selectHtml.append(" 	<ul class='ui-ddlist-options' style=''>");
		        		for(TranslateVO item : selectItems){
		        			selectHtml.append("<li data-value='"+item.getCode()+"'>"+item.getName()+"</li>");
		        		}
		        		selectHtml.append("     </ul>");
		        		selectHtml.append("     <input type='hidden' name='"+name+"' value='"+value+"' class='required'/>");
		        		selectHtml.append("</div>");
		    		}
		        } else if (list instanceof Collection) {
		        	//TODO
		        } else if (MakeIterator.isIterable(list)) {
		        	//TODO
		        }
		}catch(Exception ex){
			logger.error("CommonSelectComponent2-异常:", ex);
			logger.error(ex.getMessage());
		}
		return selectHtml.toString();
	}

	public Object getList() {
		return list;
	}

	public void setList(Object list) {
		this.list = list;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getEmptyOption() {
		return emptyOption;
	}

	public void setEmptyOption(String emptyOption) {
		this.emptyOption = emptyOption;
	}

	public String getRequired() {
		return required;
	}

	public void setRequired(String required) {
		this.required = required;
	}

	public String getHeaderKey() {
		return headerKey;
	}

	public void setHeaderKey(String headerKey) {
		this.headerKey = headerKey;
	}

	public String getHeaderValue() {
		return headerValue;
	}

	public void setHeaderValue(String headerValue) {
		this.headerValue = headerValue;
	}
}
